var bibao =12;
var round = 11;
var allPlace = [], ais = [], mens = [], bak1=[],bak2=[],bak3=[];
$(document)
        .ready(
               function(){
	               for (var i = 0; i < 15; i++)
		               $("table")
		                       .append(
		                               "<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
	            
	               for (var i = 0; i < 15; i++){
		               ais[i] = [];
		               mens[i] = [];
		               allPlace[i] = [];
	               }
	               for (var i = 0; i < 15; i++)
		               for (var j = 0; j < 15; j++)
			               ais[i][j] = mens[i][j] = evaluate(-1,i,j);
	               // 执黑
	               $("#bFirst").click(function(){
		               bclick();
	               });
	               // 执白
	               $("#wFirst").click(function(){
		               wclick();
	               });
               });
function bclick(){
	$("#wFirst").unbind("click");
	$("#bFirst").unbind("click");
	// alert("您选择了先手执黑");
	$("td").click(function(){
		bibao--;
		$(this).unbind("click");
		var b = $(this).index();
		var a = $(this).parent().index();
		var local = b * 15 + a;
		$(this).css("background-image", "url('css/tdblack.jpg')");
		if (evaluate(1, a, b) > 2200000){
			alert("游戏结束,您赢了!");
			$("td").unbind("click");
			return;
		}
		allPlace[a][b] = 1;
		fresh(a, b);
		var loc2;
		if (++round < 113)
			loc2 = noThink(-1);
		else
			loc2 = aiThink(-1, bibao);
		b = loc2[1];
		a = loc2[0];
		if (evaluate(-1, a, b) > 2200000){
			alert("游戏结束,您输了!");
			$("td").unbind("click");
		}
		$("tr").eq(a).find("td").eq(b).unbind("click");
		$("tr").eq(a).find("td").eq(b).css("background-image", "url('css/tdwhite.jpg')");
		allPlace[a][b] = -1;
		fresh(a, b);
	});
}
function wclick(){
	$("#wFirst").unbind("click");
	$("#bFirst").unbind("click");
	// alert("您选择了后手执白");
	allPlace[7][7] = -1;
	fresh(7,7);
	$("td").eq(112).css("background-image", "url('css/tdblack.jpg')")
	$("td").click(function(){
		bibao--;
		$(this).unbind("click");
		var b = $(this).index();
		var a = $(this).parent().index();
		var local = b * 15 + a;
		$(this).css("background-image", "url('css/tdwhite.jpg')");
		if (evaluate(1, a, b) > 2200000){
			alert("游戏结束,您赢了!");
			$("td").unbind("click");
			return;
		}
		allPlace[a][b] = 1;
		fresh(a, b);
		var loc2;
		if (++round < 113)
			loc2 = noThink(-1);
		else
			loc2 = aiThink( -1, bibao);
		b = loc2[1];
		a = loc2[0];
		if (evaluate(-1, a, b) > 2200000){
			alert("游戏结束,您输了!");
			$("td").unbind("click");
		}
		$("tr").eq(a).find("td").eq(b).unbind("click");
		$("tr").eq(a).find("td").eq(b).css("background-image", "url('css/tdblack.jpg')");
		allPlace[a][b] = -1;
		fresh(a, b);
	});
}
// 全棋盘分析
function nthink(sign){
	return noThink(sign);
}

function aiThink(sign,deep){
	var no = noThink(sign);
	// x>4*x*c;y*4<x*c;43胜或堵活4
	if (no[2] + no[3] * 0.2 > 70000 || no[2] > 55000 || no[3] > 100000||deep<0)
		return no;
	//
	var co = search(sign, 999);
	var op = search(0 - sign, 5000);
	var com =find(sign, 55000, 70000, co);
	for (var i = 0; i < com.length; i++){
		bak();
		var flag=judge(com[i][0], com[i][1], sign,deep);
		restore();
		if(flag)
			return com[i];
		}
	var opp = find(0 - sign, 55000,2200000,op);
	if (opp.length > 0)
		return opp[0];
	com = find(sign, 10000,55000, co);
	for (var i = 0; i < com.length; i++){
		bak();
		var flag=judge(com[i][0], com[i][1], sign,deep);
		restore();
		if(flag)
			return com[i];
	}
	
	com=find(sign,4000,10001,co);
	for (var i = 0; i < com.length; i++){
		bak();
		var flag=judge(com[i][0], com[i][1], sign,deep);
		restore();
		if(flag)
			return com[i];
	}
	
	console.log("no>>>>");
	return no;
}


//最大值
function othermax(sign,a,b){
	var arr=sign==-1?mens:ais;
	var score=0;
	for(var i=0;i<arr.length;i++)
		for(var j=0;j<arr[i].length;j++)
			if(score<arr[i][j]&&(i!=a||j!=b))
				score=arr[i][j];
	return score;
}

function restore(){
	ais = bak1.pop();
	mens = bak2.pop();
	allPlace = bak3.pop();
}
function bak(){
	var alls=[],ai=[],men=new Array();
	for (var i = 0; i < 15; i++){
		alls[i] = allPlace[i].concat();
		ai[i] = ais[i].concat();
		men[i] = mens[i].concat();
	}
	bak1.push(ai),bak2.push(men),bak3.push(alls);
}
function fresh(a,b){
	var c = a + 4 < 14 ? a + 4 : 14,d = b + 4 < 14 ? b + 4 : 14;
	var e = a - 4 > 0 ? a - 4 : 0,f = b - 4 > 0 ? b - 4 : 0;
	mens[a][b] = -1,ais[a][b] = -1;
	// 横
	for (var i = b + 1; i <= d; i++)
		if (allPlace[a][i] == -1)
			continue;
		else if (allPlace[a][i] == undefined)
			ais[a][i] = evaluate(-1, a, i);
		else
			break;
	for (var i = b - 1; i >= f; i--)
		if (allPlace[a][i] == -1)
			continue;
		else if (allPlace[a][i] == undefined)
			ais[a][i] = evaluate(-1, a, i);
		else
			break;
	// 竖
	for (var i = a + 1; i <= c; i++)
		if (allPlace[i][b] == -1)
			continue;
		else if (allPlace[i][b] == undefined)
			ais[i][b] = evaluate(-1, i, b);
		else
			break;
	for (var i = a - 1; i >= e; i--)
		if (allPlace[i][b] == -1)
			continue;
		else if (allPlace[i][b] == undefined)
			ais[i][b] = evaluate(-1, i, b);
		else
			break;
	// 捺\
	for (var i = a + 1, j = b + 1; i <= c && j <= d; i++, j++)
		if (allPlace[i][j] == -1)
			continue;
		else if (allPlace[i][j] == undefined)
			ais[i][j] = evaluate(-1, i, j);
		else
			break;
	for (var i = a - 1, j = b - 1; i >= e && j >= f; i--, j--)
		if (allPlace[i][j] == -1)
			continue;
		else if (allPlace[i][j] == undefined)
			ais[i][j] = evaluate(-1, i, j);
		else
			break;
	// 撇丿/
	for (var i = a + 1, j = b - 1; i <= c && j >= f; i++, j--)
		if (allPlace[i][j] == -1)
			continue;
		else if (allPlace[i][j] == undefined)
			ais[i][j] = evaluate(-1, i, j);
		else
			break;
	for (var i = a - 1, j = b + 1; i >= e && j <= d; i--, j++)
		if (allPlace[i][j] == -1)
			continue;
		else if (allPlace[i][j] == undefined)
			ais[i][j] = evaluate(-1, i, j);
		else
			break;
	// 横
	for (var i = b + 1; i <= d; i++)
		if (allPlace[a][i] == 1)
			continue;
		else if (allPlace[a][i] == undefined)
			mens[a][i] = evaluate(1, a, i);
		else
			break;
	for (var i = b - 1; i >= f; i--)
		if (allPlace[a][i] == 1)
			continue;
		else if (allPlace[a][i] == undefined)
			mens[a][i] = evaluate(1, a, i);
		else
			break;
	// 竖
	for (var i = a + 1; i <= c; i++)
		if (allPlace[i][b] == 1)
			continue;
		else if (allPlace[i][b] == undefined)
			mens[i][b] = evaluate(1, i, b);
		else
			break;
	for (var i = a - 1; i >= e; i--)
		if (allPlace[i][b] == 1)
			continue;
		else if (allPlace[i][b] == undefined)
			mens[i][b] = evaluate(1, i, b);
		else
			break;
	// 捺\
	for (var i = a + 1, j = b + 1; i <= c && j <= d; i++, j++)
		if (allPlace[i][j] == 1)
			continue;
		else if (allPlace[i][j] == undefined)
			mens[i][j] = evaluate(1, i, j);
		else
			break;
	for (var i = a - 1, j = b - 1; i >= e && j >= f; i--, j--)
		if (allPlace[i][j] == 1)
			continue;
		else if (allPlace[i][j] == undefined)
			mens[i][j] = evaluate(1, i, j);
		else
			break;
	// 撇丿/
	for (var i = a + 1, j = b - 1; i <= c && j >= f; i++, j--)
		if (allPlace[i][j] == 1)
			continue;
		else if (allPlace[i][j] == undefined)
			mens[i][j] = evaluate(1, i, j);
		else
			break;
	for (var i = a - 1, j = b + 1; i >= e && j <= d; i--, j++)
		if (allPlace[i][j] == 1)
			continue;
		else if (allPlace[i][j] == undefined)
			mens[i][j] = evaluate(1, i, j);
		else
			break;
}
// 下一步
function nextScore(a,b,sign){
	var c = a + 4 < 14 ? a + 4 : 14,d = b + 4 < 14 ? b + 4 : 14;
	var e = a - 4 > 0 ? a - 4 : 0,f = b - 4 > 0 ? b - 4 : 0;
	if (allPlace[a][b] == undefined)
		allPlace[a][b] = sign;
	var total = new Array();
	// 横
	for (var i = b + 1; i <= d; i++)
		if (allPlace[a][i] == sign)
			continue;
		else if (allPlace[a][i] == undefined)
			total.push([a,i,evaluate(sign, a, i)]);
		else
			break;
	for (var i = b - 1; i >= f; i--)
		if (allPlace[a][i] == sign)
			continue;
		else if (allPlace[a][i] == undefined)
			total.push([a,i,evaluate(sign, a, i)]);
		else
			break;
	// 竖
	for (var i = a + 1; i <= c; i++)
		if (allPlace[i][b] == sign)
			continue;
		else if (allPlace[i][b] == undefined)
			total.push([i,b,evaluate(sign, i, b)]);
		else
			break;
	for (var i = a - 1; i >= e; i--)
		if (allPlace[i][b] == sign)
			continue;
		else if (allPlace[i][b] == undefined)
			total.push([i,b,evaluate(sign, i, b)]);
		else
			break;

	// 捺\
	for (var i = a + 1, j = b + 1; i <= c && j <= d; i++, j++)
		if (allPlace[i][j] == sign)
			continue;
		else if (allPlace[i][j] == undefined)
			total.push([a,i,evaluate(sign, a, i)]);
		else
			break;
	for (var i = a - 1, j = b - 1; i >= e && j >= f; i--, j--)
		if (allPlace[i][j] == sign)
			continue;
		else if (allPlace[i][j] == undefined)
			total.push([a,i,evaluate(sign, a, i)]);
		else
			break;

	// 撇丿/
	for (var i = a + 1, j = b - 1; i <= c && j >= f; i++, j--)
		if (allPlace[i][j] == sign)
			continue;
		else if (allPlace[i][j] == undefined)
			total.push([i,j,evaluate(sign, i, j)]);
		else
			break;
	for (var i = a - 1, j = b + 1; i >= e && j <= d; i--, j++)
		if (allPlace[i][j] == sign)
			continue;
		else if (allPlace[i][j] == undefined)
			total.push([i,j,evaluate(sign, i, j)]);	
		else
			break;
	allPlace[a][b] = undefined;
	return total;
}
// 搜索
function search(sign,score){
	var result = [];
	var all = sign == -1 ? ais : mens;
	for (var i = 0; i < 15; i++)
		for (var j = 0; j < 15; j++){
			if (all[i][j] > score)
				result.push([i, j, all[i][j]]);
		}
	return result;
}
function find(sign,score,sc2,arr){
	var result = [];
	for (var i = 0; i < arr.length; i++){
		if (arr[i][2] > score && arr[i][2] < sc2)
			result.push(arr[i]);
	}
	return result;
}
// 位置打分
function evaluate(sign,a,b){
	var c = a + 4 < 14 ? a + 4 : 14,d = b + 4 < 14 ? b + 4 : 14;
	var e = a - 4 > 0 ? a - 4 : 0,f = b - 4 > 0 ? b - 4 : 0;
	var score = 15 - Math.abs(7 - a) - Math.abs(7 - b);
	var troop = new Array();
	for (var i = 0; i < 4; i++)
		troop[i] = "1";
	// 横
	for (var i = b + 1; i <= d; i++)
		if (allPlace[a][i] == sign)
			troop[0] += '1';
		else if (allPlace[a][i] == undefined)
			troop[0] += '0';
		else
			break;
	for (var i = b - 1; i >= f; i--)
		if (allPlace[a][i] == sign)
			troop[0] = '1' + troop[0];
		else if (allPlace[a][i] == undefined)
			troop[0] = '0' + troop[0];
		else
			break;
	// 竖
	for (var i = a + 1; i <= c; i++)
		if (allPlace[i][b] == sign)
			troop[1] += '1';
		else if (allPlace[i][b] == undefined)
			troop[1] += '0';
		else
			break;
	for (var i = a - 1; i >= e; i--)
		if (allPlace[i][b] == sign)
			troop[1] = '1' + troop[1];
		else if (allPlace[i][b] == undefined)
			troop[1] = '0' + troop[1];
		else
			break;

	// 捺\
	for (var i = a + 1, j = b + 1; i <= c && j <= d; i++, j++)
		if (allPlace[i][j] == sign)
			troop[2] += '1';
		else if (allPlace[i][j] == undefined)
			troop[2] += '0';
		else
			break;
	for (var i = a - 1, j = b - 1; i >= e && j >= f; i--, j--)
		if (allPlace[i][j] == sign)
			troop[2] = '1' + troop[2];
		else if (allPlace[i][j] == undefined)
			troop[2] = '0' + troop[2];
		else
			break;
	// 撇丿/
	for (var i = a + 1, j = b - 1; i <= c && j >= f; i++, j--)
		if (allPlace[i][j] == sign)
			troop[3] += '1';
		else if (allPlace[i][j] == undefined)
			troop[3] += '0';
		else
			break;
	for (var i = a - 1, j = b + 1; i >= e && j <= d; i--, j++)
		if (allPlace[i][j] == sign)
			troop[3] = '1' + troop[3];
		else if (allPlace[i][j] == undefined)
			troop[3] = '0' + troop[3];
		else
			break;
	// 计算评分
	for (var i = 0; i < 2; i++){
		if (/(11111)/g.test(troop[i]))
			score += 2200000;
		else if (/(011110)|(1011101)/g.test(troop[i]))
			score += 100000;
		else if (/(11110)|(10111)|(01111)|(11101)|(11011)/g.test(troop[i]))
			score += 50000;
		else if (/(0011100)|(01011000)|(00011010)/g.test(troop[i]))
			score += 6000;
		else if (/(011100)|(001110)|(010110)|(011010)/g.test(troop[i]))
			score += 5000;
		else if (/(00011000)|(0010100)/g.test(troop[i]))
			score += 2500;
		else if (/(001100)|(011000)|(000110)|(00100100)|(001010)|(010100)/g.test(troop[i]))
			score += 2000;
		else if (/(11100)|(10110)|(11010)|(00111)|(01011)|(01101)|(11001)|(10101)|(10011)|(01110)/g.test(troop[i]))
			score += 2000;
		else if (/(01010)|(00011)|(11000)|(01100)|(00110)|(10001)|(10010)|(01001)|(00101)|(10100)/g.test(troop[i]))
			score += 500;
		else if (/(000010000)/g.test(troop[i]))
			score += 300;
		else if (/(001000)|(000100)|(010000)|(000010)/g.test(troop[i]))
			score += 200;
		else if (/(00100)|(01000)|(00010)|(10000)|(00001)/g.test(troop[i]))
			score += 100;
	}
	for (var i =2; i < 4; i++){
		if (/(11111)/g.test(troop[i]))
			score += 2220000;
		else if (/(011110)|(1011101)/g.test(troop[i]))
			score += 101000;
		else if (/(11110)|(10111)|(01111)|(11101)|(11011)/g.test(troop[i]))
			score += 50500;
		else if (/(0011100)|(01011000)|(00011010)/g.test(troop[i]))
			score += 6100;
		else if (/(011100)|(001110)|(010110)|(011010)/g.test(troop[i]))
			score += 5100;
		else if (/(00011000)|(0010100)/g.test(troop[i]))
			score += 2550;
		else if (/(001100)|(011000)|(000110)|(00100100)|(001010)|(010100)/g.test(troop[i]))
			score += 2030;
		else if (/(11100)|(10110)|(11010)|(00111)|(01011)|(01101)|(11001)|(10101)|(10011)|(01110)/g.test(troop[i]))
			score += 2020;
		else if (/(01010)|(00011)|(11000)|(01100)|(00110)|(10001)|(10010)|(01001)|(00101)|(10100)/g.test(troop[i]))
			score += 550;
		else if (/(000010000)/g.test(troop[i]))
			score += 310;
		else if (/(001000)|(000100)|(010000)|(000010)/g.test(troop[i]))
			score += 210;
		else if (/(00100)|(01000)|(00010)|(10000)|(00001)/g.test(troop[i]))
			score += 110;
	}
	return score;
}
// 简单
function noThink(sign){
	var score = 0;
	var local = [];
	var temp;
	if (sign == -1)
		for (var i = 0; i < 15; i++)
			for (var j = 0; j < 15; j++){
				if (ais[i][j] == -1)
					continue;
				if(mens[i][j]<50000)
					temp=ais[i][j]+mens[i][j]/3;
				else
					temp = ais[i][j] + mens[i][j] * 0.2;
				if (score < temp){
					score = temp;
					local = [i, j,ais[i][j],mens[i][j]];
				}
			}
	else
		for (var i = 0; i < 15; i++)
			for (var j = 0; j < 15; j++){
				if (ais[i][j] == -1)
					continue;
				if(ais[i][j]<50000)
					temp=ais[i][j]/3+mens[i][j];
				else 
					temp = ais[i][j] * 0.2 + mens[i][j];
				if (score < temp){
					score = temp;
					local = [i, j, mens[i][j], ais[i][j]];

				}
			}
	return local;
}